package com.ledim.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.ledim.activity.base.LeDimBaseActivity;
import com.ledim.bean.ChatRoomBean;
import com.ledim.bean.base.BaseResultDataInfo;
import com.ledim.service.LedimApiServiceUtils;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.ToastUtil;
import com.letv.android.young.client.R;

/**
 * 修改放映厅名称\
 */
public class RoomNameActivity extends LeDimBaseActivity {

    public static final String ROOM_ID = "room_id";
    public static final String ROOM_NAME = "room_name";
    public static final int REQUESTCODE_ROOM_NAME = 200;
    private ImageView mBack;
    private TextView mFinish;
    private EditText mRoomNameEdit;
    private TextView mNameNum;
    private String mNameContent;
    private String mRoomId;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_room_name;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        mRoomId = getIntent().getStringExtra(ROOM_ID);
        mBack = (ImageView) findViewById(R.id.room_name_back);
        mFinish = (TextView) findViewById(R.id.room_name_finish);
        mRoomNameEdit = (EditText) findViewById(R.id.room_name_content);
        mNameNum = (TextView) findViewById(R.id.room_name_num);

        mBack.setOnClickListener(this);
        mFinish.setOnClickListener(this);
        mRoomNameEdit.setText(getIntent().getStringExtra(ROOM_NAME));
        mRoomNameEdit.requestFocus();
        mNameContent = mRoomNameEdit.getText().toString().trim();
        mNameNum.setText((23 - mNameContent.length()) + "");

        mRoomNameEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                mNameContent = mRoomNameEdit.getText().toString().trim();
                mNameNum.setText((23 - mNameContent.length()) + "");
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.room_name_back:
                finish();
                break;
            case R.id.room_name_finish:
                if (mNameContent == null || mNameContent.length() == 0) {
                    ToastUtil.toast("请输入放映厅名称");
                } else {
                    LedimApiServiceUtils.modifyRoomInfo(mRoomId +
                            "", mNameContent, null, new LedimHttpObserver<BaseResultDataInfo<ChatRoomBean>>() {
                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                        }

                        @Override
                        public void onNext(
                                BaseResultDataInfo<ChatRoomBean> chatRoomBeanBaseResultDataInfo) {
                            super.onNext(chatRoomBeanBaseResultDataInfo);
                            if (chatRoomBeanBaseResultDataInfo.success) {
                                ToastUtil.toast("放映厅名称修改成功");
                                Intent intent = new Intent();
                                intent.putExtra("content", mNameContent);
                                // 设置结果，并进行传送
                                RoomNameActivity.this.setResult(REQUESTCODE_ROOM_NAME, intent);
                                finish();
                            }
                        }
                    });
                }
                break;
        }
    }

}
